package com.learner.trade.repository;

import com.learner.pojo.trade.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.*;

/**
 * @author ice2020x
 * @Date: 2023/3/10
 * @Description:
 **/
@Repository
public interface OrderRepository extends JpaRepository<Order, String>, JpaSpecificationExecutor<Order> {


    /**
     * 课程id和用户id
     *
     * @param courseId 课程id
     * @param memberId 用户id
     * @return Order
     */
    Order findByCourseIdAndMemberId(String courseId, String memberId);

    /**
     * 根据订单id和用户id获取订单
     *
     * @param id       订单id
     * @param memberId 用户id
     * @return Order
     */
    Order findByIdAndMemberId(String id, String memberId);

    /**
     * 根据订单id和用户id和状态status获取订单
     *
     * @param id       订单id
     * @param memberId 用户id
     * @param status   状态
     * @return Order
     */
    Order findByMemberIdAndCourseIdAndStatus(String id, String memberId, Integer status);

    /**
     * 获取用户的所有订单
     *
     * @param memberId 用户id
     * @return Order
     */
    @Query(value = "select * from trade_order where member_id = ?1 AND is_deleted = 0 order by gmt_create desc", nativeQuery = true)
    List<Order> findMemberIdList(String memberId);


    /**
     * 删除订单 根据id和用户id
     *
     * @param userId 用户id
     * @param id     订单id
     * @return 删除的条数
     */
    @Modifying
    @Transactional(rollbackOn = Exception.class)
    int deleteByIdAndMemberId(String id, String userId);
}
